package com.zjh.photoshopstudy.app.video;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;
import com.lzy.okgo.request.base.Request;
import com.zjh.photoshopstudy.Model.CommentEntity;
import com.zjh.photoshopstudy.Model.Pager;
import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.app.main.OnGetVideoListener;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.zjh.photoshopstudy.Utils.Data.webRoot_URL;

public class VideoHttp {

    public static void getVideoComment(final VideoCommentFragment videoCommentFragment, long id) {
        String url = webRoot_URL + "comment/listByVideo.html";
        OkGo.<String>post(url)//
                .params("vId", id)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            List list = (List) map.get("data");
                            List<CommentEntity> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<CommentEntity>>() {
                            }.getType());
                            videoCommentFragment.onGetVideoCommentSuccess(list2);
                        } else {
                            videoCommentFragment.onGetVideoCommentFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public void sendComment(final VideoPresent.SendCommentCallBack callBack, File[] files, final CommentEntity commentEntity) {
        final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        PostRequest<String> postRequest = OkGo.<String>post(webRoot_URL + "/comment/add.html")//
                .params("comment", gson.toJson(commentEntity));
        for (File file : files) {
            postRequest.params("files", file);
        }
        postRequest.execute(new Callback<String>() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
            }

            @Override
            public void onSuccess(Response<String> response) {
                Gson gson = new Gson();
                Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                }.getType());
                if ((double) map.get("code") == 200.0) {
                    long id = ((Double) map.get("data")).longValue();
                    commentEntity.setId(id);
                    callBack.onSendSuccess(commentEntity);
                } else {
                    callBack.onSendFail();
                }
            }

            @Override
            public void onCacheSuccess(Response<String> response) {

            }

            @Override
            public void onError(Response<String> response) {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void uploadProgress(Progress progress) {

            }

            @Override
            public void downloadProgress(Progress progress) {

            }

            @Override
            public String convertResponse(okhttp3.Response response) throws Throwable {
                return response.body().string();
            }
        });
    }

    public void doCollection(final VideoPresent.CollectionCallBack commentCallBack, final VideoEntity mVideoEntity, final int uid) {
        String url = webRoot_URL + "video/collectVideo.html";
        OkGo.<String>get(url)//
                .params("uId", uid)
                .params("vId", mVideoEntity.getId())
                .params("isCollect", true)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            commentCallBack.onCollectionSuccess(mVideoEntity, uid);
                        } else {
                            commentCallBack.onCollectionFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public void doCancelCollection(final VideoPresent.CancelCollectionCallBack cancelCollectionCallBack, final VideoEntity mVideoEntity, final int uid) {
        String url = webRoot_URL + "video/collectVideo.html";
        OkGo.<String>get(url)//
                .params("uId", uid)
                .params("vId", mVideoEntity.getId())
                .params("isCollect", false)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            cancelCollectionCallBack.onCancelCollectionSuccess(mVideoEntity, uid);
                        } else {
                            cancelCollectionCallBack.onCancelCollectionFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public void deleteComment(final CommentAdapter.OnCommentDelete onCommentDelete, CommentEntity entity, final int position) {
        String url = webRoot_URL + "comment/delete.html";
        OkGo.<String>get(url)//
                .params("ids", entity.getId())
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        if ((double) map.get("code") == 200.0) {
                            onCommentDelete.onDeleteSuccess(position);
                        } else {
                            onCommentDelete.onDeleteFail();
                        }
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public void getVideoList(final OnGetVideoListener onGetVideoListener, Map<String, Object> map) {
        String url = webRoot_URL + "video/listForApp.html";
        final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        Pager pager = new Pager();
        pager.setFastQueryParameters(map);
        OkGo.<String>get(url)//
                .tag(this)//
                .params("gridPager", gson.toJson(pager))
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        List list = (ArrayList) map.get("exhibitDatas");
                        List<VideoEntity> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<VideoEntity>>() {
                        }.getType());
                        onGetVideoListener.getVideoListSuccess(list2);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }

    public void getVideo(final VideoPresent videoPresent, long id) {
        String url = webRoot_URL + "video/watchVideo.html";
        final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        OkGo.<String>get(url)//
                .tag(this)//
                .params("id", id)
                .execute(new Callback<String>() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Map<String, Object> map = gson.fromJson(response.body(), new TypeToken<Map<String, Object>>() {
                        }.getType());
                        Object map1 = ((Map<String, Object>) map.get("data")).get("videoEntity");
                        VideoEntity videoEntity = gson.fromJson(gson.toJson(map1), new TypeToken<VideoEntity>() {
                        }.getType());
                        videoPresent.getVideoSuccess(videoEntity);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {

                    }

                    @Override
                    public void onError(Response<String> response) {

                    }

                    @Override
                    public void onFinish() {

                    }

                    @Override
                    public void uploadProgress(Progress progress) {

                    }

                    @Override
                    public void downloadProgress(Progress progress) {

                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }
}
